


interface ExportInfo {
    CnFileName: string; // 文件名
    Content: number;// 进度
    FileName: string // 文件名，格式为  fileName_timestamp
    Path?: string // 文件生成后的路径
}

function tmsExport($ngBootbox: BootboxService, $rootScope: ImainRootScope, commonService: ICommonService, routeService: routeService): ng.IDirective {
    return {
        restrict: 'E',
        templateUrl: './src/directives/tms-export/tms-export.html',
        transclude: true,

        link: ($scope) => {



            $rootScope.excelExportList = [];

            const excelExportListStr = window.localStorage.getItem('excelExportList')
            if (excelExportListStr) {
                $rootScope.excelExportList = JSON.parse(excelExportListStr)
            }


            $rootScope.$on('export', (e, data: ExportInfo) => {
                if (isNewItem(data)) {
                    addNewItem(data)
                } else {
                    updateItem(data)
                }


            })


            function addNewItem(data: ExportInfo) {
                const item = {
                    skey: data.FileName,
                    CnFileName: data.CnFileName,
                    time: normaltime(data.FileName.split('_')[1]),
                    IsTrue: data.Content !== -1,
                    ProgressValue: data.Content
                }
                $rootScope.excelExportList.push(item);

                // 新增导出消息后，清除空队列
                let l;
                for (let i = 0; l = $rootScope.excelExportList.length, l > i; i++) {
                    if ($rootScope.excelExportList[i].IsFakeInfo) {
                        $rootScope.excelExportList.splice(i, 1);
                        break;
                    }
                }

                window.localStorage.setItem('excelExportList', JSON.stringify($rootScope.excelExportList))

                aplyView()
            }


            function updateItem(data: ExportInfo) {
                $rootScope.excelExportList.forEach(item => {
                    if (item.skey == data.FileName) {
                        item.ProgressValue = data.Content;
                        item.IsTrue = (data.Content != -1);
                        item.Path = data.Path ? data.Path : null
                    }
                })
                window.localStorage.setItem('excelExportList', JSON.stringify($rootScope.excelExportList))

                aplyView()
            }




            //unix时间戳转化为常用时间
            function normaltime(timer) {
                let neda: any = (timer - 621355968000000000) / 10000000;
                let neDate = new Date(parseInt(neda) * 1000);
                //console.log(commonService.transformTime(neDate,"MMddHHmm"));
                return commonService.transformTime(neDate, "MM/dd HH:mm:ss")

            }
            /**手动刷新页面view */
            function aplyView() {
                /**手动刷新页面view */
                if (!$scope.$$phase) {
                    $scope.$apply(() => { $rootScope.excelExportList });
                }
            }

            /**
             * 
             * @param data 判断新获取的融云消息是不是新增的导出信息
             */
            function isNewItem(data) {

                if (data.Content > 20) {
                    return false;
                }
                const list = $rootScope.excelExportList.filter(item => {
                    return item.skey == data.FileName
                })
                return (list.length == 0)
            }

            $rootScope.downloadExcel = function (path, key) {
                window.location.href = path;

                $rootScope.excelExportList = $rootScope.excelExportList.filter(item => {
                    return item.skey != key;
                })
                window.localStorage.setItem('excelExportList', JSON.stringify($rootScope.excelExportList))


            }
            $rootScope.selectCancel = function (key, IsFakeInfo) {
                if (IsFakeInfo) {
                    $ngBootbox.alert('文件正在生成中，暂时无法取消');
                    return;
                }

                $rootScope.excelExportList = $rootScope.excelExportList.filter(item => {
                    return item.skey != key;
                })
                window.localStorage.setItem('excelExportList', JSON.stringify($rootScope.excelExportList))
            }




        }

    }
}


tmsExport['$inject'] = ["$ngBootbox", "$rootScope", "commonService", "routeService"]
angular.module('tmsExport', []).directive('tmsExport', tmsExport);